package com.zzh.custom.nine;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zzh.custom.R;
import com.zzh.tools.listener.ToolOnClickListener;
import com.zzh.tools.system.ToolScreen;

import java.util.List;

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2021-01-27
 * @desc
 */
public class NinePicView extends ViewGroup {

    public interface OnCirclePicItemLensenter {
        void onSelectItem(NinePicView circlePicView, ImageView view, int position, NinePicBean circlePicBean);
    }

    private static final int ZERO = 0;
    private static final int ONE = 1;
    private static final int FOUR = 4;
    private static final int NINE = 9;

    public static final int ONE_NINE = 10; // 只有一张的时候也按照九宫格展示
    public static final int ONE_SELF = 11; // 只有一张的时候按照自己的尺寸比例直行
    private int oneModel = ONE_SELF;

    private int model = ZERO;

    /**
     * 九宫格 和 四宫格时的图片展示宽度
     */
    private int singleWidth = 0;

    /**
     * 多图的时候的间隔
     */
    private int interval = 20;
    /**
     * 传入的数据
     */
    private List<NinePicBean> mValues;
    private int dip2pxWidth = 0;
    /**
     * 单一一张图片是 图片的宽和高
     */
    private int mOneWidth = 200;
    private int mOneHeight = 200;

    private float mWidth;
    private int mHeight;

    private int dip2px30 = 0;
    private int dip2px15 = 0;

    private OnCirclePicItemLensenter mOnCirclePicItemLensenter;

    private boolean isPoint;

    public void setPoint(boolean point) {
        isPoint = point;
    }

    public int itemPosition;


    public void setOnCirclePicItemLensenter(OnCirclePicItemLensenter onCirclePicItemLensenter) {
        mOnCirclePicItemLensenter = onCirclePicItemLensenter;
    }

    public NinePicView(Context context) {
        this(context, null);
    }

    public NinePicView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NinePicView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public List<NinePicBean> getValues() {
        return mValues;
    }

    private void init() {
        interval = ToolScreen.dip2px(getContext(), 5f);
        int screenWidth = ToolScreen.getScreenWidth((Activity) getContext());
        dip2pxWidth = screenWidth / 3;
        mOneWidth = (int) (Float.valueOf(screenWidth) / 375 * 230);
        mOneHeight = mOneWidth;
        dip2px30 = ToolScreen.dip2px(getContext(), 30f);
        dip2px15 = ToolScreen.dip2px(getContext(), 15f);
        for (int i = 0; i < 9; i++) {
            RelativeLayout view = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.nine_pic_view_item, null);
            view.setTag(i);
            view.setId(i + 100);
            view.setOnClickListener(mOnClickListener);
            addView(view);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (model == ZERO) {
        } else if (model == ONE) {
            if (oneModel == ONE_SELF) {
                ViewGroup view = (ViewGroup) getChildAt(0);
                view.layout(0 + getPaddingLeft(), 0, mOneWidth, mOneHeight);
                view.getChildAt(0).layout(0, 0, mOneWidth, mOneHeight);
                view.getChildAt(1).layout(mOneWidth - dip2px30, mOneHeight - dip2px15, mOneWidth, mOneHeight);
            } else if (oneModel == ONE_NINE) {
                ViewGroup view = (ViewGroup) getChildAt(0);
                view.layout(0 + getPaddingLeft(),
                        0,
                        singleWidth,
                        singleWidth);
                view.getChildAt(0).layout(0, 0, singleWidth, singleWidth);
                view.getChildAt(1).layout(singleWidth - dip2px30, singleWidth - dip2px15, singleWidth, singleWidth);
            } else {
                ViewGroup view = (ViewGroup) getChildAt(0);
                view.layout(0 + getPaddingLeft(), 0, mOneWidth, mOneHeight);
                view.getChildAt(0).layout(0, 0, mOneWidth, mOneHeight);
                view.getChildAt(1).layout(mOneWidth - dip2px30, mOneHeight - dip2px15, mOneWidth, mOneHeight);
            }
        } else if (model == FOUR) {
            int count = mValues.size();
            for (int i = 0; i < count; i++) {
                ViewGroup view = (ViewGroup) getChildAt(i);
                if (count == 3) {
                    view.layout(singleWidth * i + interval * i + getPaddingLeft(),
                            0,
                            singleWidth * (i + 1) + interval * i,
                            singleWidth);
                } else if (i < 2) {
                    view.layout(singleWidth * i + interval * i + getPaddingLeft(),
                            0,
                            singleWidth * (i + 1) + interval * i,
                            singleWidth);
                } else {
                    view.layout(singleWidth * (i - 2) + interval * (i - 2) + getPaddingLeft(),
                            singleWidth + interval,
                            singleWidth * (i - 1) + interval * (i - 2),
                            singleWidth * 2 + interval);
                }
                view.getChildAt(0).layout(0, 0, singleWidth, singleWidth);
                view.getChildAt(1).layout(singleWidth - dip2px30, singleWidth - dip2px15, singleWidth, singleWidth);
            }
        } else {
            int count = mValues.size();
            if (count > 9) {
                count = 9;
            }
            for (int i = 0; i < count; i++) {
                ViewGroup view = (ViewGroup) getChildAt(i);
                if (i < 3) {
                    view.layout(singleWidth * i + interval * i + getPaddingLeft(),
                            0,
                            singleWidth * (i + 1) + interval * i,
                            singleWidth);
                } else if (i < 6) {
                    view.layout(singleWidth * (i - 3) + interval * (i - 3) + getPaddingLeft(),
                            singleWidth + interval,
                            singleWidth * (i - 2) + interval * (i - 3),
                            singleWidth * 2 + interval);
                } else {
                    view.layout(singleWidth * (i - 6) + interval * (i - 6) + getPaddingLeft(),
                            (singleWidth + interval) * 2,
                            singleWidth * (i - 5) + interval * (i - 6),
                            singleWidth * 3 + interval * 2);
                }
                view.getChildAt(0).layout(0, 0, singleWidth, singleWidth);
                view.getChildAt(1).layout(singleWidth - dip2px30, singleWidth - dip2px15, singleWidth, singleWidth);
            }
        }
    }

    public void setData(List<NinePicBean> values) {
        this.mValues = values;
        if (mValues == null || values.size() <= 0) {
            model = ZERO;
            setVisibility(View.GONE);
            return;
        }
        int countAll = this.mValues.size();
        if (countAll > 9) {
            for (int i = countAll - 1; i >= 9; i--) {
                this.mValues.remove(i);
            }
        }


        int count = this.mValues.size();
        model = count > 4 ? NINE : count > 1 ? FOUR : ONE;
        count = count <= 9 ? count : 9;
        for (int i = 0; i < count; i++) {
            ViewGroup view = (ViewGroup) getChildAt(i);
            view.setVisibility(View.VISIBLE);
            ImageView imageView = (ImageView) view.getChildAt(0);
            TextView textView = (TextView) view.getChildAt(1);
            NinePicBean value = mValues.get(i);
            int picWidth = value.getWidth();
            int picHeight = value.getHeight();
            String url = value.getUrl();
//            imageView.getOptions().setDecodeGifImage(false).setShaper(new RoundRectImageShaper(ToolScreen.dip2px(getContext(), 5)));
            if (!TextUtils.isEmpty(url) && url.toLowerCase().contains(".gif")) {
                textView.setText("动图");
                textView.setVisibility(View.VISIBLE);
            } else if (picHeight > picWidth * 2) {
                textView.setText("长图");
                textView.setVisibility(View.VISIBLE);
            } else {
                textView.setVisibility(View.GONE);
            }
            if (count == 1 && oneModel != ONE_NINE) {
                mOneHeight = picHeight == picWidth ? mOneWidth
                        : picWidth > picHeight ? (int) (Float.valueOf(mOneWidth) / 230 * 175)
                        : (int) (Float.valueOf(mOneWidth) / 230 * 310);
//                imageView.getOptions().setShapeSize(new ShapeSize(mOneWidth, mOneHeight, ImageView.ScaleType.CENTER_CROP));
//                imageView.displayImage(mValues.get(i).getUrl());
                NineImageLoaderGenerator.getNineImageLoader().displayImage(imageView, mValues.get(i).getUrl());
            } else {
//                imageView.getOptions().setShapeSize(new ShapeSize(dip2pxWidth, dip2pxWidth, ImageView.ScaleType.CENTER_CROP));
//                imageView.displayImage(mValues.get(i).getUrl());
                NineImageLoaderGenerator.getNineImageLoader().displayImage(imageView, mValues.get(i).getUrl());
            }
        }
        for (int i = count; i < 9; i++) {
            getChildAt(i).setVisibility(View.GONE);
        }
        setVisibility(View.VISIBLE);
        invalidate();
    }


    public void setDataMaxThree(List<NinePicBean> values) {
        if (values != null && values.size() > 3) {
            this.mValues = values.subList(0, 3);
        } else {
            this.mValues = values;
        }
        if (mValues == null || mValues.size() <= 0) {
            model = ZERO;
            setVisibility(View.GONE);
            return;
        }
        int countAll = this.mValues.size();
        if (countAll > 9) {
            for (int i = countAll - 1; i <= 9; i--) {
                this.mValues.remove(i);
            }
        }


        int count = this.mValues.size();
        model = count > 4 ? NINE : count > 1 ? FOUR : ONE;
        count = count <= 9 ? count : 9;
        for (int i = 0; i < count; i++) {
            ViewGroup view = (ViewGroup) getChildAt(i);
            view.setVisibility(View.VISIBLE);
            ImageView imageView = (ImageView) view.getChildAt(0);
            TextView textView = (TextView) view.getChildAt(1);
            NinePicBean value = mValues.get(i);
            int picWidth = value.getWidth();
            int picHeight = value.getHeight();
            String url = value.getUrl();
//            imageView.getOptions().setDecodeGifImage(false).setShaper(new RoundRectImageShaper(ToolScreen.dip2px(getContext(), 5)));
            if (!TextUtils.isEmpty(url) && url.toLowerCase().contains(".gif")) {
                textView.setText("动图");
                textView.setVisibility(View.VISIBLE);
            } else if (picHeight > picWidth * 2) {
                textView.setText("长图");
                textView.setVisibility(View.VISIBLE);
            } else {
                textView.setVisibility(View.GONE);
            }
            if (count == 1 && oneModel != ONE_NINE) {
                mOneHeight = picHeight == picWidth ? mOneWidth
                        : picWidth > picHeight ? (int) (Float.valueOf(mOneWidth) / 230 * 175)
                        : (int) (Float.valueOf(mOneWidth) / 230 * 310);
//                imageView.getOptions().setShapeSize(new ShapeSize(mOneWidth, mOneHeight, ImageView.ScaleType.CENTER_CROP));
//                imageView.displayImage(mValues.get(i).getUrl());
                NineImageLoaderGenerator.getNineImageLoader().displayImage(imageView, mValues.get(i).getUrl());
            } else {
//                imageView.getOptions().setShapeSize(new ShapeSize(dip2pxWidth, dip2pxWidth, ImageView.ScaleType.CENTER_CROP));
//                imageView.displayImage(mValues.get(i).getUrl());
                NineImageLoaderGenerator.getNineImageLoader().displayImage(imageView, mValues.get(i).getUrl());
            }
        }
        for (int i = count; i < 9; i++) {
            getChildAt(i).setVisibility(View.GONE);
        }
        setVisibility(View.VISIBLE);
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        singleWidth = (int) ((mWidth - getPaddingLeft() - getPaddingRight() - 2 * interval) / 3);
//        dip2pxWidth = singleWidth;
        if (mValues == null || mValues.size() <= 0) {
            return;
        }
        if (model == ZERO) {
        } else if (model == ONE) {
            mHeight = oneModel == ONE_SELF ? mOneHeight : oneModel == ONE_NINE ? singleWidth : mOneHeight;
        } else if (model == FOUR) {
            int count = mValues.size();
            mHeight = count <= 3 ? singleWidth : singleWidth * 2 + interval;
        } else {
            int count = mValues.size();
            mHeight = count > 6 ? (int) mWidth : count > 3 ? singleWidth * 2 + interval : singleWidth;
        }
        setMeasuredDimension((int) mWidth, mHeight);
    }

    private View.OnClickListener mOnClickListener = new ToolOnClickListener() {
        @Override
        public void onClick(View view) {
            super.onClick(view);
            if (id == -1) {
                return;
            }
            int tag = (int) view.getTag();
            if (mOnCirclePicItemLensenter != null) {
                NinePicBean circlePicBean = mValues.get(tag);
                circlePicBean.setPosition(tag);
                circlePicBean.setmRight(mOneWidth);
                circlePicBean.setmBottom(mOneHeight);
                int position = -1;
                if (getTag() != null) {
                    position = (int) getTag();
                }
                ImageView imageView = (ImageView) ((ViewGroup) view).getChildAt(0);

                mOnCirclePicItemLensenter.onSelectItem(NinePicView.this, imageView, position, circlePicBean);
            }
        }
    };


    public static class NinePicBean {

        private String url;
        private int drId;
        private int width = 0;
        private int height = 0;


        private int mRight;
        private int mBottom;

        public String getUrl() {
            return url;
        }

        public int getmRight() {
            return mRight;
        }

        public void setmRight(int mRight) {
            this.mRight = mRight;
        }

        public int getmBottom() {
            return mBottom;
        }

        public void setmBottom(int mBottom) {
            this.mBottom = mBottom;
        }

        private int position;

        public int getPosition() {
            return position;
        }

        public void setPosition(int position) {
            this.position = position;
        }


        public int getDrId() {
            return drId;
        }

        public int getHeight() {
            return height;
        }

        public int getWidth() {
            return width;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public void setDrId(int drId) {
            this.drId = drId;
        }

        public void setHeight(int height) {
            this.height = height;
        }

        public void setWidth(int width) {
            this.width = width;
        }

        @Override
        public String toString() {
            return "NinePicBean{" +
                    "url='" + url + '\'' +
                    ", drId=" + drId +
                    ", width=" + width +
                    ", height=" + height +
                    ", mRight=" + mRight +
                    ", mBottom=" + mBottom +
                    ", position=" + position +
                    '}';
        }
    }

    public void setOneModel(int oneModel) {
        this.oneModel = oneModel;
    }
}